package com.shenmazong.review.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.review.mapper.TbAskPaperDetailMapper;
import com.shenmazong.review.mapper.TbUserReviewMapper;
import com.shenmazong.review.pojo.TbAskPaper;
import com.shenmazong.review.pojo.TbUserReview;
import com.shenmazong.review.service.TbAskPaperService;
import com.shenmazong.review.mapper.TbAskPaperMapper;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.ReviewInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class TbAskPaperServiceImpl extends ServiceImpl<TbAskPaperMapper, TbAskPaper>
    implements TbAskPaperService{

    @Autowired
    TbAskPaperMapper tbAskPaperMapper;

    @Autowired
    TbAskPaperDetailMapper tbAskPaperDetailMapper;

    @Autowired
    TbUserReviewMapper tbUserReviewMapper;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Override
    public ResultResponse getReviewPaperList() {
        List<TbAskPaper> tbAskPapers = tbAskPaperMapper.selectList(null);
        return ResultResponse.SUCCESS(tbAskPapers);
    }

    @Override
    public ResultResponse beginReviewByPaperId(ReviewInfoVo infoVo) {
        //--1 生成本次**评测ID**
        TbUserReview tbUserReview = new TbUserReview();
        tbUserReview.setUserId(infoVo.getUserId());
        tbUserReview.setPaperId(infoVo.getPaperId());

        // 填充用于字段
        // TODO
        tbUserReviewMapper.insert(tbUserReview);

        //--2 获取这个评测问卷的所有问题的ID
        List<Integer> questionIds = tbAskPaperDetailMapper.getQuestionIdListByPaperId(infoVo.getPaperId());
        if(questionIds==null || questionIds.size()==0) {
            return ResultResponse.FAILED(-1, "问卷没有问题");
        }

        //--2 把问卷ID列表放入redis
        // 通过redis模拟一个问题队列：左侧放入问题：右侧获取问题
        String key = "review_"+tbUserReview.getReviewId();
        questionIds.forEach(item -> {
            redisTemplate.opsForList().leftPush(key, item);
        });


        return ResultResponse.SUCCESS(tbUserReview);
    }
}




